package cn.flying.cloud.oauth.web.oauth;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.oauth.entity.OauthClientDetail;
import cn.flying.cloud.oauth.server.configuration.details.UserDetail;
import cn.flying.cloud.oauth.service.OauthClientDetailService;

/**
 * 客户端请求处理器
 *
 * @author: admin
 * @date: 2023年04月11日 14:58
 * @version: 1.0
 */
@Controller
@RequestMapping("/oauth/client/")
public class OauthClientDetailController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OauthClientDetailService oauthClientDetailService;

    /**
     * 客户端列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model) {
        SecurityContext context = SecurityContextHolder.getContext();
        UserDetail userDetail = (UserDetail) context.getAuthentication().getPrincipal();
        model.addAttribute("userId", userDetail.getUserId());
        model.addAttribute("username", userDetail.getUsername());
        model.addAttribute("name", userDetail.getName());
        return "oauth-index";
    }

    /**
     * 分页查询客户端信息表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    @ResponseBody
    public PageResponse<OauthClientDetail> page(@RequestBody PageRequest<OauthClientDetail> request) {
        return oauthClientDetailService.page(request);
    }

    /**
     * 列表查询客户端信息表数据
     *
     * @param oauthClientDetail
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Rt list(@RequestBody OauthClientDetail oauthClientDetail) {
        List<OauthClientDetail> list = oauthClientDetailService.list(oauthClientDetail);
        return Rt.success(list);
    }

    /**
     * 查询客户端信息表详情数据
     *
     * @param oauthClientDetail
     * @return
     */
    @PostMapping("detail")
    @ResponseBody
    public Rt detail(OauthClientDetail oauthClientDetail) {
        return Rt.success(oauthClientDetailService.detail(oauthClientDetail));
    }

    /**
     * 根据主键ID查询客户端信息表详情数据
     *
     * @param id
     * @return
     */
    @PostMapping("getById")
    @ResponseBody
    public Rt getById(Long id) {
        OauthClientDetail oauthClientDetail = oauthClientDetailService.getById(id);
        return Rt.success(oauthClientDetail);
    }

    /**
     * 保存客户端信息表数据
     *
     * @param oauthClientDetail
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    public Rt save(@RequestBody OauthClientDetail oauthClientDetail) {
        if (Objects.isNull(oauthClientDetail.getClientId())) {
            // 新增
            oauthClientDetailService.create(oauthClientDetail);
        } else {
            // 更新
            oauthClientDetailService.update(oauthClientDetail);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除客户端信息表数据
     *
     * @param oauthClientDetail
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Rt delete(OauthClientDetail oauthClientDetail) {
        oauthClientDetailService.delete(oauthClientDetail);
        return Rt.success("删除成功！");
    }
}
